﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;

namespace FrameWork.web.Module.ITMS.KnowledgeBase.KBQuery
{
    public partial class Manager : System.Web.UI.Page
    {
        Int32 IDX = (Int32)Common.sink("IDX", MethodType.Get, 10, 0, DataType.Int);
        string CMD = (string)Common.sink("CMD", MethodType.Get, 10, 1, DataType.Str);
        int KindID;
      
        protected void Page_Load(object sender, EventArgs e)
        {
            FrameWorkPermission.CheckPagePermission(CMD);
            KindID = (int)Common.sink("KID", MethodType.Get, 255, 0, DataType.Int);
            //KID.Text = KindID.ToString().Trim();
            //KName = (string)Common.sink("KName", MethodType.Get, 255, 0, DataType.Str);
            //if (KName.Trim() != "")
            //{
            //    HeadMenuWebControls1.HeadTitleTxt = "知识库[" + KName.Trim() + "]";
            //}

            if (!Page.IsPostBack)
            {
                OnStart();
                
            }
        }

        /// <summary>
        /// 处理新增操作
        /// </summary>
        private void DoneNew()
        {
            if (KindID == 0)
            {
                Button1.Enabled = false;
                EventMessage.MessageBox(1, "错误信息", string.Format("没有选择知识库类别，请选择!", IDX), Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
                return;
            }

            string KName = BusinessFacade.its_KnowledgeBaseKindDisp(KindID).K_KName;
            if (KName.Trim() != "")
            {
                HeadMenuWebControls1.HeadTitleTxt = "知识库[" + KName.Trim() + "]";
            }
            txtKID.Text = KindID.ToString();
            txtStatus.Text = "1";
            sys_UserTable ut = BusinessFacade.sys_UserDisp(Common.Get_UserID);
            txtUserID.Text = ut.UserID.ToString();
            txtUserName.Text = ut.U_CName.Trim();
            
        }
        /// <summary>
        /// 开始操作
        /// </summary>
        private void OnStart()
        {
            its_KnowledgeBaseEntity ut = BusinessFacade.its_KnowledgeBaseDisp(IDX);
            OnStartData(ut);
            switch (CMD)
            {
                case "New":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "增加知识库";
                    Hidden_Disp();
                    DoneNew();
                    break;
                case "List":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "查看知识库";
                    Hidden_Input();
                    ButtonOption.Visible = false;
                    AddEditButton();
                    break;
                case "Edit":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "修改知识库";
                    Hidden_Disp();
                    AddDeleteButton();
                    break;
                case "Delete":
                    ut.DataTable_Action_ = DataTable_Action.Delete;
                    string URL = string.Format("Default.aspx?KID={0}", ut.KID);

                    if (BusinessFacade.its_KnowledgeBaseInsertUpdateDelete(ut) > 0)
                    {
                        EventMessage.MessageBox(1, "删除成功", string.Format("删除ID:{0}成功!", IDX), Icon_Type.OK, Common.GetHomeBaseUrl(URL));
                    }
                    else
                    {
                        EventMessage.MessageBox(1, "删除失败", string.Format("删除ID:{0}失败!", IDX), Icon_Type.Error, Common.GetHomeBaseUrl(URL));
                    }
                    break;
                default:
                    EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    break;
            }
        }

        /// <summary>
        /// 增加修改按钮
        /// </summary>
        private void AddEditButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Edit;
            bi.ButtonName = "知识库";
            bi.ButtonUrl = string.Format("?CMD=Edit&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);
        }


        /// <summary>
        /// 增加删除按钮
        /// </summary>
        private void AddDeleteButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Delete;
            bi.ButtonName = "知识库";
            bi.ButtonUrlType = UrlType.JavaScript;
            bi.ButtonUrl = string.Format("DelData('?CMD=Delete&IDX={0}')", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);

            HeadMenuButtonItem bi1 = new HeadMenuButtonItem();
            bi1.ButtonPopedom = PopedomType.List;
            bi1.ButtonIcon = "back.gif";
            bi1.ButtonName = "返回";
            bi1.ButtonUrl = string.Format("?CMD=List&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi1);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="ut"></param>
        private void OnStartData(its_KnowledgeBaseEntity ut)
        {
            if (ut.KID > 0)
            {
                string KName = BusinessFacade.its_KnowledgeBaseKindDisp(ut.KID).K_KName;
                if (KName.Trim() != "")
                {
                    HeadMenuWebControls1.HeadTitleTxt = "知识库[" + KName.Trim() + "]";
                }
            }

            txtKID.Text = KID_Disp.Text = ut.KID.ToString();
            txtStatus.Text = Status_Disp.Text = ut.Status.ToString();
            txtSubject.Text = Subject_Disp.Text = ut.Subject.ToString();
            txtContent.Text = Content_Disp.Text = ut.Content.ToString();
            txtKeyWord.Text = KeyWord_Disp.Text = ut.KeyWord.ToString();
            txtAttachment.Text = Attachment_Disp.Text = ut.Attachment.ToString();
            txtDocNo.Text = DocNo_Disp.Text = ut.DocNo.ToString();
            txtUserID.Text = UserID_Disp.Text = ut.UserID.ToString();
            txtUserName.Text = UserName_Disp.Text = ut.UserName.ToString();
            txtCollateDAT.Text = CollateDAT_Disp.Text = ut.CollateDAT.ToString();
        }

        /// <summary>
        /// 隐藏输入框
        /// </summary>
        private void Hidden_Input()
        {
            txtKID.Visible = false;
            txtStatus.Visible = false;
            txtSubject.Visible = false;
            txtContent.Visible = false;
            txtKeyWord.Visible = false;
            txtAttachment.Visible = false;
            txtDocNo.Visible = false;
            txtUserID.Visible = false;
            txtUserName.Visible = false;
            txtCollateDAT.Visible = false;
        }

        /// <summary>
        /// 隐藏显示框
        /// </summary>
        private void Hidden_Disp()
        {
            KID_Disp.Visible = false;
            Status_Disp.Visible = false;
            Subject_Disp.Visible = false;
            Content_Disp.Visible = false;
            KeyWord_Disp.Visible = false;
            Attachment_Disp.Visible = false;
            DocNo_Disp.Visible = false;
            UserID_Disp.Visible = false;
            UserName_Disp.Visible = false;
            CollateDAT_Disp.Visible = false;
        }

        /// <summary>
        /// 增加/修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            int KID_Value = (int)Common.sink(txtKID.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            int Status_Value = (int)Common.sink(txtStatus.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            string Subject_Value = (string)Common.sink(txtSubject.UniqueID, MethodType.Post, 200, 0, DataType.Str);
            string Content_Value = (string)Common.sink(txtContent.UniqueID, MethodType.Post, 0, 0, DataType.Str);
            string KeyWord_Value = (string)Common.sink(txtKeyWord.UniqueID, MethodType.Post, 50, 0, DataType.Str);
            string Attachment_Value = (string)Common.sink(txtAttachment.UniqueID, MethodType.Post, 255, 0, DataType.Str);
            string DocNo_Value = (string)Common.sink(txtDocNo.UniqueID, MethodType.Post, 20, 0, DataType.Str);
            int UserID_Value = (int)Common.sink(txtUserID.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            string UserName_Value = (string)Common.sink(txtUserName.UniqueID, MethodType.Post, 20, 0, DataType.Str);
            DateTime CollateDAT_Value = (DateTime)Common.sink(txtCollateDAT.UniqueID, MethodType.Post, 20, 0, DataType.Dat);

            string URL = string.Format("Default.aspx?KID={0}", KID_Value);

            its_KnowledgeBaseEntity ut = BusinessFacade.its_KnowledgeBaseDisp(IDX);
            ut.KID = KID_Value;
            ut.Status = Status_Value;
            ut.Subject = Subject_Value;
            ut.Content = Content_Value;
            ut.KeyWord = KeyWord_Value;
            ut.Attachment = Attachment_Value;
            ut.DocNo = DocNo_Value;
            ut.UserID = UserID_Value;
            ut.UserName = UserName_Value;
            ut.CollateDAT = CollateDAT_Value;
            if (CMD == "New")
            {
                ut.DataTable_Action_ = DataTable_Action.Insert;
            }
            else if (CMD == "Edit")
            {
                ut.DataTable_Action_ = DataTable_Action.Update;
            }
            else
            {
                EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl(URL));
            }
            Int32 rInt = BusinessFacade.its_KnowledgeBaseInsertUpdateDelete(ut);
            if (rInt > 0)
            {
                string OpTxt = string.Format("增加知识库成功!(ID:{0})", rInt);
                if (ut.DataTable_Action_ == DataTable_Action.Update)
                    OpTxt = string.Format("修改知识库成功!(ID:{0})", IDX);
                EventMessage.MessageBox(1, "操作成功", OpTxt, Icon_Type.OK, Common.GetHomeBaseUrl(URL));
            }
            else if (rInt == -2)
            {
                EventMessage.MessageBox(1, "操作失败", "操作失败,存在相同的键值(用户名/数据)!", Icon_Type.Alert, Common.GetHomeBaseUrl(URL));
            }
            else
            {
                EventMessage.MessageBox(1, "操作失败", string.Format("操作失败,返回值:{0}!", rInt), Icon_Type.Error, Common.GetHomeBaseUrl(URL));
            }
        }
    }
}
